package com.thread.sync.sync07;

import java.util.Queue;
import java.util.concurrent.ConcurrentMap;

//worker抽象类,处理master分配的任务并将结果返回给master
public abstract class Worker implements Runnable {
    //引用master任务容器 ,适用于高并发队列
    private Queue<MyTask> tasks;
    //引用master任务返回数据结果集合  并发容器
    private ConcurrentMap<String, Object> result;

    @Override
    public void run() {
        while (true) {
            MyTask task = this.tasks.poll();//取任务并处理
            if (task == null) break;
            //处理子类一的业务逻辑并获取结果
            Object output = Worker01.handle(task);
            this.result.put(Integer.toString(task.getId()), output);
        }
    }

    //定义业务处理逻辑方法,具体业务由子类实现
    public static Object handle(MyTask task) {
        return null;
    }

    public void setTasks(Queue<MyTask> tasks) {
        this.tasks = tasks;
    }

    public void setResult(ConcurrentMap<String, Object> result) {
        this.result = result;
    }
}
